package com.ctcemti.mdm.framework.aop;

import com.ctcemti.mdm.common.util.RedisUtil;
import com.ctcemti.mdm.project.sys.service.ICommonService;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
/**
 * @ClassName RedisAop
 * @Description aop类用于控制组织的redis更新
 * @Author bxf
 * @Date 22:56 2020/2/7
 * @Version V1.0
 **/
@Aspect
@Component
public class RedisAop {

    @Autowired
    ICommonService commonService;

    @Autowired
    RedisUtil redisUtil;

    @Pointcut("execution(public * com.ctcemti.mdm.project.sys.controller.CompanyController.insertCompany(..))")
    public void cutInsertOrUpdate(){
    }

    @Pointcut("execution(public * com.ctcemti.mdm.project.sys.controller.CompanyController.delete(..))")
    public void delete(){
    }

    @Pointcut("execution(public * com.ctcemti.mdm.project.sys.controller.ProjectController.delete(..))")
    public void deletePro(){
    }

    @Pointcut("execution(public * com.ctcemti.mdm.project.sys.controller.ProjectController.importPros(..))")
    public void importPros(){
    }

    @Pointcut("cutInsertOrUpdate() || delete() || importPros() || deletePro()")
    public void cutAll(){
    }

    @After("cutAll()")
    public void doAfter(JoinPoint joinPoint){
        RequestAttributes ra =RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra=(ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
        String personId = request.getParameter("personId")!=null?request.getParameter("personId").toString():"2";
        if(redisUtil.hasKey(personId)){
            redisUtil.del(personId);
            commonService.insertComRelationToRedisByPersonId(personId);
        }
        commonService.insertComRelationToRedisByPersonId(personId);
    }


}
